<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'local';
$setting=core_setting('upload');
if($_RQ['op']=='local_file'){ 
	//新的文件上传接口
	if(!$_FILES['up_file']){
		exi('没获取到上传的文件！也许视频太大，请联系管理员配置好服务器环境！','error');
	}
	if(is_array($_FILES['up_file']['error'])){
		foreach($_FILES['up_file']['error'] as $code){
			if($code!=0){
				exi('上传失败，可能是图片太大超出服务器限制，请联系系统管理员！','error');
			}
		}
	}elseif($_FILES['up_file']['error']){
		exi('上传失败，可能是图片太大超出服务器限制，请联系系统管理员！','error');
	}
	$type=$_RQ['type'];
	if(!in_array($type,array('image','video','audio','other'))){
		//标准附件类型
		exi('上传文件类型错误！'.$type,'error');
	}
	$group_id=intval($_RQ['group_id']);
	$group_id2=intval($_RQ['group_id2']);
	$res=array();
	foreach($_FILES['up_file']['name'] as $key=>$name){
		$ext=strtolower(substr(strrchr($name,'.'),1));
		$len=strlen($ext)+1;
		$filename=substr($name,0,-$len);
		if(!strexists($setting[$type.'_ext'],';'.$ext.';')){
			exi('上传文件类型不符合要求(上传的文件类型在：系统-附件里面可以设置)！'.$ext,'error');
		}
		/*if($type=='image' && $ext=='jpg'){
			$exif=exif_read_data($_FILES['up_file']['tmp_name'][$key]);
			if($exif['Orientation'] && $exif['Orientation']!=1){
				$image=imagecreatefromjpeg($_FILES['up_file']['tmp_name'][$key]);
				switch($exif['Orientation']){
					case 8:
						$image = imagerotate($image,90,0);
						break;
					case 3:
						$image = imagerotate($image,180,0);
						break;
					case 6:
						$image = imagerotate($image,-90,0);
						break;
				}
				imagejpeg($image,$_FILES['up_file']['tmp_name'][$key]);
				//file_put_contents($_FILES['up_file']['tmp_name'][$key],$image)
			}
		}*/
		if(!$group_id){
			$group_id=$_SESSION['this']=='manage'?-1:-2;
		}
		//$filename=iconv('gbk',"utf-8//IGNORE",$filename);
		$res[]=cfc('files')->source_save($_FILES['up_file']['tmp_name'][$key],$type,$ext,$filename,'',$group_id,'','',$group_id2);
	}
	if($_SESSION['os']=="wxapp" && $type=='video'){
		//小程序上传视频，这里获取首图
		$video_url=tomedia($res['0']);
			$first_url=cloud_request('cloud.video_first',array('url'=>$video_url));
			if($first_url=='error'){
				exi($res);
			}
			$data=file_get_contents($first_url);
			if(!$data){
				exi($res);
			}
			$image_res=cfc('files')->source_save($data,'image','jpg','视频首图','',$group_id,'','',$group_id2);
			$new_first=array(
				'uniacid'=>$_SESSION['uniacid'],
				'video'=>$res['0'],
				'image'=>$image_res,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_attachment_first_image',$new_first);
			$res[]=$image_res;
			exi($res);
	}
	exi($res);
}
/*
微信上传图片
*/
if($_RQ['op']=='wechat_image'){
	$serverid=trim($_RQ['serverid']);
	if(!$serverid){
		exi('参数错误！','error');
	}
	$url=cfc('account')->get_media($serverid);
	$resp = cfc('http')->ihttp_get($url);
	if (is_error($resp)) {
		exi('提取文件失败, 错误信息: '.$resp['message'],'error');
	}
	if (intval($resp['code']) != 200) {
		exi('提取文件失败, 未找到该资源文件.','error');
	}
	$ext = '';
	$type =$_RQ['type'];
	$type = in_array($type, array('image','audio','video')) ? $type : 'image';
	if ($type == 'image') {
		switch ($resp['headers']['Content-Type']){
			case 'application/x-jpg':
			case 'image/jpeg':
				$ext = 'jpg';
				break;
			case 'image/png':
				$ext = 'png';
				break;
			case 'image/gif':
				$ext = 'gif';
				break;
			default:
				$result['message'] = '提取资源失败, 资源文件类型错误.';
				die(json_encode($result));
				break;
		}
	} else {
		exi('提取资源失败, 仅支持图片提取.','error');
	}
	
	if(!$group_id){
		$group_id=$_SESSION['this']=='manage'?-1:-2;
	}
	$filename = cfc('files')->source_save($resp['content'],$type,$ext,$serverid,'',$group_id,'','',$group_id2);
	exi($filename);
}
if($_RQ['op']=='local'){
	$post=$_RQ;
	$group_id=$_SESSION['this']=='manage'?-1:-2;
	if($post['from_video']){
		$check=pdo_getcolumn('core_attachment_first_image',array('video'=>$post['from_video']),'image');
		if($check){
			exi($check);
		}
		//if($post['filedata']=='data:,'){
			$video_url=tomedia($post['from_video']);
			$first_url=cloud_request('cloud.video_first',array('url'=>$video_url));
			if($first_url=='error'){
				exi('');
			}
			$data=file_get_contents($first_url);
			if(!$data){
				exi('');
			}
			$res=cfc('files')->source_save($data,'image','jpg','视频首图','',$group_id,'','',$group_id2);
			$new_first=array(
				'uniacid'=>$_SESSION['uniacid'],
				'video'=>$post['from_video'],
				'image'=>$res,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('core_attachment_first_image',$new_first);
			exi($res);
		//}
	}
	$data=$post['filedata'];
	$filename=$post['filename'];
	$filename=str_replace('\\','/',$filename);
	$filename=substr(strrchr($filename,'/'),1);
	$ext=strtolower(substr(strrchr($filename,'.'),1));
	$len=strlen($ext)+1;
	$filename=substr($filename,0,-$len);
	
	$type=$post['type'];
	
	if(!in_array($type,array('image','video','audio','other'))){
		//标准附件类型
		exi('上传文件类型错误！'.$type,'error');
		
	}
	if($type=='other' && preg_match('/[^\w\.-]/',$filename)){//文件名只允许字母、数字、下划线、点
		exi('文件名不合法(文件名只允许字母、数字、下划线、点)！','error');
	}
	if(!strexists($setting[$type.'_ext'],';'.$ext.';')){
		exi('上传文件类型不符合要求(上传的文件类型在：系统-附件里面可以设置)！'.$ext,'error');
	}
	$data=explode(';',$data);
	$base64=substr($data['1'],7);
	$data=base64_decode($base64);
	//$filename=iconv('gbk',"utf-8//IGNORE",$filename);
	$res=cfc('files')->source_save($data,$type,$ext,$filename,'',$group_id,'','',$group_id2);
	$new_first=array(
		'uniacid'=>$_SESSION['uniacid'],
		'video'=>$post['from_video'],
		'image'=>$res,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('core_attachment_first_image',$new_first);
	exi($res);
}
if($_RQ['op']=='remote'){
	$post=get_postdata();
	$url=trim($post['url']);
	$type=trim($post['type']);
	if($type!='image'){
		exi('上传文件类型错误！','error');
	}
	$resp = cfc('http')->ihttp_get($url);
	if (is_error($resp)) {
		exi('提取文件失败, 错误信息: '.$resp['message'],'error');
	}
	if (intval($resp['code']) != 200) {
		exi('提取文件失败: 未找到该资源文件.','error');
	}
	$ext = '';
		switch ($resp['headers']['Content-Type']){
			case 'application/x-jpg':
			case 'image/jpeg':
				$ext = 'jpg';
				break;
			case 'image/png':
				$ext = 'png';
				break;
			case 'image/gif':
				$ext = 'gif';
				break;
			default:
				exi('提取资源失败，资源类型错误！','error');
				break;
		}
	if (intval($resp['headers']['Content-Length']) > $setting['image'] * 1024*1024) {
		exi('上传的媒体文件过大!','error');
	}
	$originname = pathinfo($url, PATHINFO_BASENAME);
	$md5=md5($resp['content']);
	$filename=pdo_getcolumn('core_attachment',array('md5'=>$md5,'uniacid'=>$_SESSION['uniacid']),'attachment');
	if(!$filename){
		$group_id=$_SESSION['this']=='manage'?-1:-2;
		$originfilename=str_replace(strrchr($originname,'.'),'',$originname);
		//$originfilename=iconv('gbk',"utf-8//IGNORE",$originfilename);
		$filename = cfc('files')->source_save($resp['content'],$type,$ext,$originfilename,'',$group_id,'','',$group_id2);
	}
	exi($filename);
}